package com.feihong.txgw.ui.activity.product;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AMapNaviListener;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.AMapNaviViewListener;
import com.amap.api.navi.AMapNaviViewOptions;
import com.amap.api.navi.enums.NaviType;
import com.amap.api.navi.model.AMapCalcRouteResult;
import com.amap.api.navi.model.AMapLaneInfo;
import com.amap.api.navi.model.AMapModelCross;
import com.amap.api.navi.model.AMapNaviCameraInfo;
import com.amap.api.navi.model.AMapNaviCross;
import com.amap.api.navi.model.AMapNaviInfo;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.AMapNaviRouteNotifyData;
import com.amap.api.navi.model.AMapNaviTrafficFacilityInfo;
import com.amap.api.navi.model.AMapServiceAreaInfo;
import com.amap.api.navi.model.AimLessModeCongestionInfo;
import com.amap.api.navi.model.AimLessModeStat;
import com.amap.api.navi.model.NaviInfo;
import com.amap.api.navi.model.NaviLatLng;
import com.autonavi.tbt.TrafficFacilityInfo;
import com.blankj.utilcode.util.LogUtils;
import com.feihong.txgw.R;
import com.feihong.txgw.dialog.ChooseMapDialog;
import com.feihong.txgw.message.LoginMessage;
import com.feihong.txgw.ui.activity.base.BaseActivity;
import com.feihong.txgw.utils.ScreenUtil;
import com.feihong.txgw.utils.gaode.ErrorInfo;
import com.feihong.txgw.utils.gaode.TTSController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Sabirjan on 2017/6/15.
 * TXGW2.
 * 飞宏电子
 * 导航
 */

public class DaoHangActivity extends BaseActivity implements AMapNaviListener, AMapNaviViewListener {

    private static final String TAG = "导航";
    protected AMapNaviView mAMapNaviView;
    protected AMapNavi mAMapNavi;
    protected TTSController mTtsManager;
    //算路终点坐标
    protected NaviLatLng mEndLatlng;
    //算路起点坐标
    protected NaviLatLng mStartLatlng;// = new NaviLatLng(43.89170311111,87.61491511111)
    //存储算路起点的列表
    protected final List<NaviLatLng> sList = new ArrayList<NaviLatLng>();
    //存储算路终点的列表
    protected final List<NaviLatLng> eList = new ArrayList<NaviLatLng>();
    protected List<NaviLatLng> mWayPointList;


    private TextView mChooseMapTv;

    private RelativeLayout mTitleLayout;
    private ImageView mBackBtn;
    private TextView mTitleTv;
    private RelativeLayout.LayoutParams mTitleParams;
    private ChooseMapDialog mChooseMapDialog;

    private List<String> mPositions;

    @Override
    public void setTheme() {

    }

    @Override
    public int setLayoutID() {
        return R.layout.activity_daohang;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

        mAMapNaviView = (AMapNaviView) findViewById(R.id.navi_view);
        mAMapNaviView.onCreate(savedInstanceState);
        mAMapNaviView.setAMapNaviViewListener(this);
        AMapNaviViewOptions options = mAMapNaviView.getViewOptions();
        // 如果不设置 周围建筑物就会显示为3D类型
        options.setTilt(0);
        options.setLayoutVisible(false);
        mAMapNaviView.setViewOptions(options);
        mPositions = new ArrayList<>();
        mChooseMapDialog = new ChooseMapDialog(this, null);
//
        mTitleLayout = (RelativeLayout) findViewById(R.id.top_back_layout);
        mBackBtn = (ImageView) findViewById(R.id.btn_back);
        mTitleTv = (TextView) findViewById(R.id.txt_pagetitle);
        mChooseMapTv = new TextView(this);
        mChooseMapTv.setVisibility(View.VISIBLE);
        mChooseMapTv.setText("导航");
        mChooseMapTv.setTextColor(getResources().getColor(R.color.black));
        if (mChooseMapTv.getParent() == null) {
            mTitleLayout.addView(mChooseMapTv);
        }
        mTitleParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mTitleParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        mTitleParams.addRule(RelativeLayout.CENTER_VERTICAL);
        mChooseMapTv.setPadding(ScreenUtil.dip2px(this, 10), 0, ScreenUtil.dip2px(this, 15), 0);
        mChooseMapTv.setLayoutParams(mTitleParams);
        mChooseMapTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mChooseMapDialog.showDialog();
            }
        });


        mBackBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Intent att = getIntent();
        String lat = att.getStringExtra("end_lat");
        String lng = att.getStringExtra("end_lng");
        String start_lat = att.getStringExtra("start_lat");
        String start_lng = att.getStringExtra("start_lng");
        String endName = att.getStringExtra("addr");
        mTitleTv.setText("目的地位置");
        mTitleTv.setMaxEms(10);
        mPositions.add(start_lat);
        mPositions.add(start_lng);
        mPositions.add(lat);
        mPositions.add(lng);
        mPositions.add(endName);
        mChooseMapDialog.setData(mPositions);
        mEndLatlng = new NaviLatLng(Double.parseDouble(lat), Double.parseDouble(lng));
        mStartLatlng = new NaviLatLng(Double.parseDouble(start_lat),
                Double.parseDouble(start_lng));

//        mAMapNaviView.setNaviMode(AMapNaviView.NORTH_UP_MODE);
        //实例化语音引擎

        mTtsManager = TTSController.getInstance(getApplicationContext());
        mTtsManager.init();

        mAMapNavi = AMapNavi.getInstance(getApplicationContext());
        mAMapNavi.addAMapNaviListener(this);
        mAMapNavi.addAMapNaviListener(mTtsManager);
        //设置模拟导航的行车速度
        mAMapNavi.setEmulatorNaviSpeed(75);
        sList.add(mStartLatlng);
        eList.add(mEndLatlng);
    }

    @Override
    public void bindData() {

    }

    @Override
    public void onUserLogin(LoginMessage loginMessage) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        mAMapNaviView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mAMapNaviView.onPause();

//        仅仅是停止你当前在说的这句话，一会到新的路口还是会再说的
        mTtsManager.stopSpeaking();
//
//        停止导航之后，会触及底层stop，然后就不会再有回调了，但是讯飞当前还是没有说完的半句话还是会说完
//        mAMapNavi.stopNavi();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAMapNaviView.onDestroy();
        //since 1.6.0 不再在naviview destroy的时候自动执行AMapNavi.stopNavi();请自行执行
        mAMapNavi.stopNavi();
        mAMapNavi.destroy();
        mTtsManager.destroy();
    }

    @Override
    public void onInitNaviFailure() {
        Toast.makeText(this, "init navi Failed", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onInitNaviSuccess() {
        /**
         * 方法: int strategy=mAMapNavi.strategyConvert(congestion, avoidhightspeed, cost, hightspeed, multipleroute); 参数:
         * @congestion 躲避拥堵
         * @avoidhightspeed 不走高速
         * @cost 避免收费
         * @hightspeed 高速优先
         * @multipleroute 多路径
         *
         *  说明: 以上参数都是boolean类型，其中multipleroute参数表示是否多条路线，如果为true则此策略会算出多条路线。
         *  注意: 不走高速与高速优先不能同时为true 高速优先与避免收费不能同时为true
         */
        int strategy = 0;
        try {
            //再次强调，最后一个参数为true时代表多路径，否则代表单路径
            strategy = mAMapNavi.strategyConvert(true, false, false, false, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mAMapNavi.calculateDriveRoute(sList, eList, mWayPointList, strategy);

    }


    @Override
    public void onStartNavi(int type) {
        //开始导航回调
        LogUtils.i(TAG + ": onInitNaviSuccess:开始导航回调 ");
    }

    @Override
    public void onTrafficStatusUpdate() {
        //
    }

    @Override
    public void onLocationChange(AMapNaviLocation location) {
        //当前位置回调
        LogUtils.i(TAG + ": onInitNaviSuccess:当前位置回调");
    }

    @Override
    public void onGetNavigationText(int type, String text) {
        //播报类型和播报文字回调
    }

    @Override
    public void onGetNavigationText(String s) {

    }


  /*  @Override
    public void onGetNavigationText(String s) {

    }*/

    @Override
    public void onEndEmulatorNavi() {
        //结束模拟导航
    }

    @Override
    public void onArriveDestination() {
        //到达目的地
    }


    @Override
    public void onCalculateRouteFailure(int errorInfo) {
        //路线计算失败
        LogUtils.e(TAG + ": --------------------------------------------");
        LogUtils.i(TAG + ": 路线计算失败：错误码=" + errorInfo + ",Error Message= " + ErrorInfo.getError(errorInfo));
        LogUtils.i(TAG + ": 错误码详细链接见：http://lbs.amap.com/api/android-navi-sdk/guide/tools/errorcode/");
        LogUtils.e(TAG + ": --------------------------------------------");
        Toast.makeText(this, "errorInfo：" + errorInfo + ",Message：" + ErrorInfo.getError(errorInfo), Toast.LENGTH_LONG).show();
    }

    @Override
    public void onReCalculateRouteForYaw() {
        //偏航后重新计算路线回调
    }

    @Override
    public void onReCalculateRouteForTrafficJam() {
        //拥堵后重新计算路线回调
    }

    @Override
    public void onArrivedWayPoint(int wayID) {
        //到达途径点
    }

    @Override
    public void onGpsOpenStatus(boolean enabled) {
        //GPS开关状态回调
    }

    @Override
    public void onNaviSetting() {
        //底部导航设置点击回调
    }

    @Override
    public void onNaviMapMode(int isLock) {
        //地图的模式，锁屏或锁车
    }

    @Override
    public void onNaviCancel() {
        finish();
    }


    @Override
    public void onNaviTurnClick() {
        //转弯view的点击回调
    }

    @Override
    public void onNextRoadClick() {
        //下一个道路View点击回调
    }

    @Override
    public void onScanViewButtonClick() {
        //全览按钮点击回调
    }

    @Deprecated
    @Override
    public void onNaviInfoUpdated(AMapNaviInfo naviInfo) {
        //过时
    }

    @Override
    public void updateCameraInfo(AMapNaviCameraInfo[] aMapCameraInfos) {

    }

    @Override
    public void updateIntervalCameraInfo(AMapNaviCameraInfo aMapNaviCameraInfo, AMapNaviCameraInfo aMapNaviCameraInfo1, int i) {

    }

    @Override
    public void onServiceAreaUpdate(AMapServiceAreaInfo[] amapServiceAreaInfos) {
        for (int i = 0; i < amapServiceAreaInfos.length; i++) {
            LogUtils.i(TAG, amapServiceAreaInfos[i].getName() + "  " + amapServiceAreaInfos[i].getRemainDist() + "  " + amapServiceAreaInfos[i].getType());
        }
    }

    @Override
    public void onNaviInfoUpdate(NaviInfo naviinfo) {
        //导航过程中的信息更新，请看NaviInfo的具体说明
    }

    @Override
    public void OnUpdateTrafficFacility(TrafficFacilityInfo trafficFacilityInfo) {
        //已过时
    }

    @Override
    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo aMapNaviTrafficFacilityInfo) {
        //已过时
    }

    @Override
    public void showCross(AMapNaviCross aMapNaviCross) {
        //显示转弯回调
    }

    @Override
    public void hideCross() {
        //隐藏转弯回调
    }

    @Override
    public void showModeCross(AMapModelCross aMapModelCross) {

    }

    @Override
    public void hideModeCross() {

    }

    @Override
    public void showLaneInfo(AMapLaneInfo[] laneInfos, byte[] laneBackgroundInfo, byte[] laneRecommendedInfo) {
        //显示车道信息

    }

    @Override
    public void showLaneInfo(AMapLaneInfo aMapLaneInfo) {

    }

    @Override
    public void hideLaneInfo() {
        //隐藏车道信息
    }

    @Override
    public void onCalculateRouteSuccess(int[] ints) {

    }


/*    @Override
    public void onCalculateMultipleRoutesSuccess(int[] ints) {

    }*/


    @Override
    public void notifyParallelRoad(int i) {
        if (i == 0) {
//            Toast.makeText(this, "当前在主辅路过渡", Toast.LENGTH_SHORT).show();
            LogUtils.i(TAG + ": 当前在主辅路过渡");
            return;
        }
        if (i == 1) {
//            Toast.makeText(this, "当前在主路", Toast.LENGTH_SHORT).show();

            LogUtils.i(TAG + ": 当前在主路");
            return;
        }
        if (i == 2) {
//            Toast.makeText(this, "当前在辅路", Toast.LENGTH_SHORT).show();

            LogUtils.i(TAG + ": 当前在辅路");
        }
    }

    @Override
    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] aMapNaviTrafficFacilityInfos) {
        //更新交通设施信息
    }

    @Override
    public void updateAimlessModeStatistics(AimLessModeStat aimLessModeStat) {
        //更新巡航模式的统计信息
    }


    @Override
    public void updateAimlessModeCongestionInfo(AimLessModeCongestionInfo aimLessModeCongestionInfo) {
        //更新巡航模式的拥堵信息
    }

    @Override
    public void onPlayRing(int i) {

    }

    @Override
    public void onCalculateRouteSuccess(AMapCalcRouteResult aMapCalcRouteResult) {
        //多路径算路成功回调
        //路线计算成功
        LogUtils.i(TAG + ": 路线计算成功");
        mAMapNavi.startNavi(NaviType.GPS);
    }

    @Override
    public void onCalculateRouteFailure(AMapCalcRouteResult aMapCalcRouteResult) {

    }

    @Override
    public void onNaviRouteNotify(AMapNaviRouteNotifyData aMapNaviRouteNotifyData) {

    }


    @Override
    public void onLockMap(boolean isLock) {
        //锁地图状态发生变化时回调
    }

    @Override
    public void onNaviViewLoaded() {
        LogUtils.i(TAG + ": 导航页面加载成功");
        LogUtils.i(TAG + ": 请不要使用AMapNaviView.getMap().setOnMapLoadedListener();会overwrite导航SDK内部画线逻辑");
    }

    @Override
    public void onMapTypeChanged(int i) {

    }

    @Override
    public void onNaviViewShowMode(int i) {

    }

    @Override
    public boolean onNaviBackClick() {
        return false;
    }


}